<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="maximum-scale=1.0, minimum-scale=1.0, user-scalable=0, initial-scale=1.0, width=device-width"/>
<meta name="format-detection" content="telephone=no, email=no, date=no, address=no">
<title>Hello APP</title>
<link rel="stylesheet" type="text/css" href="../../css/aui-flex.css" />
<link rel="stylesheet" type="text/css" href="../../css/aui.css" />
<link rel="stylesheet" type="text/css" href="../../css/common.css" />
<style>
body{
	background: #F3F6F8;
}
.ui-loader{
	display: none;
}
.addr-list{
	padding: 0.8rem 1rem;
	margin-bottom: 0.4rem;
	position: relative;
	zoom: 1;
	overflow: hidden;
}
.addr-list .dTag{
	display: none;
}
.comFlexItem{
	min-height: 5rem;
}
.name{
	padding-bottom: 0.6rem;
}
.edit{
	padding-left: 1rem;
}

.conAddr{
	font-size: 0.8rem;
	color: #010101;
}
/*默认时*/
.default{
	border-left: 0.2rem solid #E73350;
	/*padding-left: 0.8rem;*/
	 position: relative;
	 background: #fff;
}
.default .dTag{
	width: 1.6rem;
	height: 0.8rem;
	line-height: 0.8rem;
	font-size:0.5rem;
	display: inline-block;
	text-align: center;
	line-height: 16px;
	border-radius: 2px;
	border: 1px solid #E73350;
	color: #E73350;
	vertical-align: 0.1rem;
	margin-left: 0.8rem;
}
.addit {
			height: 5rem;
		 line-height: 5rem;
		 position: absolute;
		 right: 0rem;
		 width: 0rem;
		 top: 0;
		 color: #fff;
		 text-align: center;
 }
 .addit > div{
 	width: 4rem;
	/*height: 5rem;*/

	background: #adadad;

	font-size:0.7rem;
		 text-align: center;
 }
  .addit > div.del{
  	background: #e73350;
		color: #fff;
  }
	.notData{
	  text-align: center;
	  min-height: 100%;
	  position: absolute;
	  width: 100%;
		background: #fff;
	}
	.notData img{
	  height: 7rem;
	  margin: 1rem auto;
	}
</style>
</head>
<body>
<div class="aui-content">
	<div class="notData aui-hide aui-flex-row aui-flex-middle bg-white">
    <img src="../../image/notdata/12.png" class="notImg" alt="">
		<div class="color-ad aui-font-size-14">还没有收货地址哦</div>
  </div>
		<div id="list_html">

		</div>
	<script type="text/x-dot-template" id="list_tpl">
	{{ for(var i in it) { }}
		<div class="dataId {{= it[i].address_default == 1 ? 'default' : '' }} relative" id="{{=it[i].address_userid}}" data-id="{{=it[i].address_id}}" tapmode onclick="selectThis(this);">
			<div class="addr-list comFlexItem">
				<div class="flex-auto conAddr">
					<div class="name"><span class="addr_name">{{=it[i].address_username}}</span><span class="aui-padded-l-10 addr_phone">{{=it[i].address_phone}}</span><span class="dTag">默认</span></div>
					<div class="addr color-35 aui-font-size-12" data-prov="{{=it[i].address_prov}}" data-city="{{=it[i].address_city}}" data-district="{{=it[i].address_district}}" data-detail="{{=it[i].address_detail}}">
							{{=it[i].address_prov}}
							{{=it[i].address_city}}
							{{=it[i].address_district}}
							{{=it[i].address_detail}}
					</div>
				</div>
				<div></div>
				<div class="edit" tapmode onclick="edit_Address(this)"><img src="../../image/mine/edit.png" style="height:1rem" alt=""></div>
			</div>
			<div class="addit aui-flex-col">
				<div class="" id="defaultText" tapmode onclick="setDefault(this)">{{= it[i].address_default == 1 ? '取消默认' : '设为默认' }}</div>
				<div class="del" tapmode onclick="delAddr(this)">删除</div>
			</div>
		</div>
	{{}}}
	</script>
</div>
</body>
<script type="text/javascript" src="../../script/import.js"></script>
<script type="text/javascript" src="../../script/common.js"></script>
<script type="text/javascript" src="../../script/jquery.min.js"></script>
<script type="text/javascript" src="../../script/jquery.mobile-1.4.5.min.js"></script>
<script type="text/javascript">
	apiready = function(){
		api.parseTapmode();
		getAdrList()
	};
	//设为默认地址
	function setDefault(el){
			var _this = $$(el);
			var item = _this.parents('.dataId');
			var itemId = item.data('id');
			api.ajax({
				url: BASE_URL + 'App/User/defaultAddress',
				method: 'post',
				data: {
					values: {
						userid: getUserId(),
						address_id: itemId
					}
				}
			},function(ret, err){
				if (ret) {
					if(ret.code == 1){
						toast("设置成功");
						$$('#defaultText').text('取消默认')
						getAdrList();
					}else{
						toast(ret.msg);
						$$('#defaultText').text('设为默认')
					}
				}
			});
	}
	function delAddr(el){
		var _this = $$(el);
		var item = _this.parents('.dataId');
		var itemId = item.data('id');
		var confirmMaskTxt = ['确认删除收货地址？','取消','确认删除'];
		openConfirmMask(confirmMaskTxt,'del('+itemId+')');
	}
	function del(Id){
		// var _this = $$('.aui-content .dataId[data-id="'+ Id +'"]');
		api.ajax({
			url: BASE_URL + 'App/User/deleteAddress',
			method: 'post',
			data: {
				values: {
					address_id: Id
				}
			}
		},function(ret, err){
			if (ret) {
				if(ret.code == 1){
					setTimeout(function(){
						getAdrList()
					},500);
					toast(ret.msg);
				}else{
					toast(ret.msg);
				}
			}
		});

	}
// 编辑收货地址
function edit_Address(el){
	var _this = $$(el);
	var item = _this.parents('.dataId');
	var itemId = item.data('id');
	api.openWin({
			name: 'add_address_win',
			url: 'add_address_win.html',
			pageParam: {
					editAddr: '编辑收货地址',
					itemId: itemId
			}
	});
	api.parseTapmode();
}

function getAdrList(){
	api.ajax({
		url: BASE_URL + 'App/User/addressList',
		method: 'post',
		data: {
			values: {
				userid: getUserId()
			}
		}
	},function(ret, err){
		if (ret) {
			if(ret.code == 1){
				$$('.notData').addClass('aui-hide');
				var tpl = $$('#list_tpl').html();
				var tempFn = doT.template(tpl);
				var list_data = ret.data;
				$$('#list_html').html(tempFn(list_data));
				api.parseTapmode();
			$('.addr-list').on("swipeleft", function () {//左滑显示隐藏按键
						$(this).animate({left: '-8rem'}, 200, 'linear');
						$(this).siblings('.addit').animate({width: '8rem'}, 200, 'linear');
						// $(this).parent('li').siblings('li').find('.addr-list').animate({left: '0'}, 200).siblings('.addit').animate({width: '0'}, 200);
				});
				$('.addr-list').on("swiperight", function () {//右滑恢复
						$(this).animate({left: '0'}, 200, 'linear');
						$(this).siblings('.addit').animate({width: '0rem'}, 200, 'linear');
				});
		}else{
			$$('.notData').removeClass('aui-hide');
		}
	}
	});

}

function selectThis(el){
	if( api.pageParam.fname == 'confirm_frm' ){
		var _this = $$(el);
		var name = _this.find('.addr_name').text();
		var mobile = _this.find('.addr_phone').text();
		var address_prov = _this.find('.addr').data('prov');
		var address_city = _this.find('.addr').data('city');
		var address_district = _this.find('.addr').data('district');
		var address_detail = _this.find('.addr').data('detail');
		api.sendEvent({
			name: 'selectAddressEvent',
			extra: {
				info: {
					name: name,
					mobile: mobile,
					prov: address_prov,
					city :address_city,
					district: address_district,
					address: address_detail
				}
			}
		});
		setTimeout(function(){
			api.closeWin();
		},50);
	}
}
</script>
</html>
